package rexsee.cartoon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.view.View;

public class CartoonView extends View {

	private final ArrayList<CartoonInterface> mCartoons = new ArrayList<CartoonInterface>();
	private final Handler mHandler = new Handler();
	private final Runnable mRunnable = new Runnable() {
		@Override
		public void run() {
			if (mIsPlaying) postInvalidate();
		}
	};

	private Runnable mOnStop = null;

	private int mInterval = 25;
	private boolean mIsPlaying = false;

	public CartoonView(Context context) {
		super(context);
		setClickable(true);
	}

	public void play() {
		mIsPlaying = true;
		postInvalidate();
	}
	public void stop() {
		mIsPlaying = false;
		if (mOnStop != null) mOnStop.run();
	}
	public boolean isPlaying() {
		return mIsPlaying;
	}

	public void setOnStopListener(Runnable onStop) {
		mOnStop = onStop;
	}

	public int getInterval() {
		return mInterval;
	}
	public void setInterval(int interval) {
		if (interval > 0) mInterval = interval;
	}

	public void add(CartoonInterface cartoon) {
		mCartoons.add(cartoon);
	}
	public void remove(CartoonInterface cartoon) {
		mCartoons.remove(cartoon);
	}
	public int size() {
		return mCartoons.size();
	}
	public void clear() {
		mCartoons.clear();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (mCartoons.size() == 0) return;
		ArrayList<CartoonUnit> all = new ArrayList<CartoonUnit>();
		for (int i = 0; i < mCartoons.size(); i++) {
			ArrayList<CartoonUnit> frames = mCartoons.get(i).getUnits(this);
			if (frames == null || frames.size() == 0) continue;
			for (int j = 0; j < frames.size(); j++) {
				CartoonUnit unit = frames.get(j);
				if (unit != null) all.add(unit);
			}
		}
		if (mIsPlaying) {
			mHandler.postDelayed(mRunnable, mInterval);
		}
		Collections.sort(all, new CompratorForCartoonUnit());
		for (int i = 0; i < all.size(); i++) {
			all.get(i).draw(canvas);
		}
	}

	public static interface CartoonInterface {
		public ArrayList<CartoonUnit> getUnits(CartoonView view);
	}

	public static class CartoonUnit {

		public Bitmap bitmap = null;
		public Paint paint = null;
		public Rect srcRect = null;
		public Rect targetRect = null;
		public int zIndex = 0;
		public boolean followSrcRect = true;
		public boolean autoRecycle = false;

		public void draw(Canvas canvas) {
			if (bitmap == null) return;
			Rect src = (srcRect == null) ? new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()) : srcRect;
			Rect target = (targetRect == null) ? ((followSrcRect) ? srcRect : new Rect(0, 0, canvas.getWidth(), canvas.getHeight())) : targetRect;
			canvas.drawBitmap(bitmap, src, target, paint);
			if (autoRecycle) {
				bitmap.recycle();
				bitmap = null;
			}
		}

	}

	public static class CompratorForCartoonUnit
			implements
			Comparator<CartoonUnit> {
		@Override
		public int compare(CartoonUnit cartoon1, CartoonUnit cartoon2) {
			long diff = cartoon1.zIndex - cartoon2.zIndex;
			if (diff > 0) return 1;
			else if (diff == 0) return 0;
			else return -1;
		}
		@Override
		public boolean equals(Object obj) {
			return true;
		}
	}

}
